<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="net.zdsoft.wpchat.support.util.PageUtils" %>
<%@ page import="net.zdsoft.wpchat.support.entity.*" %>
<%@ page import="net.zdsoft.keel.util.*" %>
<%@ page import="java.util.*" %>
<%@ page import="net.zdsoft.wpchat.support.action.advanced.SqlExecuteTaskAction"%>
<%
SqlExecuteTaskAction action =(SqlExecuteTaskAction)PageUtils.getAction();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN" xml:lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css" media="all">@import url("../../css/style.css");</style>
<script type="text/javascript" language="javascript" src="../../script/prototype.js"></script>
<script type="text/javascript" language="javascript" src="../../script/buffalo.js"></script>
<script type="text/javascript" language="javascript" src="../../script/replyutils_s.js"></script>
<script type="text/javascript" language="javascript" src="../../script/utils.js"></script>
<script type="text/javascript" language="javascript">


function showResultPage(pageNum){
  if (document.taskPerformForm.taskText.value == ""){
    alert("请先输入执行语句");
    return;
  }
  var form = document.taskPerformForm;
  form.resultIndex.value = pageNum;
  encodeTask();
  form.submit();
}

var paramArray = new Array();<%

String[] wildcards = action.getWildcards();
String[] wildCardValues = action.getWildcardValues();
if (wildcards != null){
  for (int i = 0; i < wildcards.length; i++){%>
paramArray["<%=wildcards[i] %>"] = "<%=wildCardValues[i]%>";<% 
  }
}%>

function parseParam(isInit){
  var taskContent = document.taskPerformForm.taskText.value;
  var reg = /\\$\{.+?\}/ig;
  var result = taskContent.match(reg);
  var paramDiv = $("paramDiv");
  if (result && result.length > 0){
    var innerHtml = "";
    var validParamList = new Array();
    var index = 0;
    
    //整理重复的参数
    for (var i = 0; i < result.length; i++){
      var isExists = false;
      for (var j = 0; j < validParamList.length; j++){
        if (validParamList[j] == result[i]){
          isExists = true;
          break;
        }
      }
      if (!isExists){
        validParamList[index++] = result[i];
      }
    }
    
    innerHtml += "<table id=\"dataTable\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\" style=\"margin-left:0px;width: 100%\">";
    innerHtml += "<tr class=\"titleTr\"><td>参数名</td><td>参数值</td></tr>";
    for (var i = 0; i < validParamList.length; i++){    
      innerHtml += "<tr><td>";
      innerHtml += validParamList[i] + "：";
      innerHtml += "</td><td>";
      innerHtml += "<input type=\"hidden\" name=\"wildcards\" value=\"" + validParamList[i] + "\" />";
      innerHtml += "<input type=\"text\" name=\"wildcardValues\" value=\"";
      if (!isInit){
        innerHtml += (paramArray[validParamList[i]] == null ? "": paramArray[validParamList[i]]);
      }
      innerHtml += "\" style=\"width: 300px\"/>";
      
      innerHtml += "</td></tr>";
    }
    innerHtml += "</table>";
    paramDiv.innerHTML = innerHtml;
    paramDiv.style.display = "inline";
  }else {
    paramDiv.style.display = "none";
  }
}


</script>
<script language=javascript>
var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function base64encode(str) {
  var out, i, len;
  var c1, c2, c3;
  len = str.length;
  i = 0;
  out = "";
  while(i < len) {
    c1 = str.charCodeAt(i++) & 0xff;
    if(i == len)
    {
      out += base64EncodeChars.charAt(c1 >> 2);
      out += base64EncodeChars.charAt((c1 & 0x3) << 4);
      out += "==";
      break;
    }
    c2 = str.charCodeAt(i++);
    if(i == len)
    {
      out += base64EncodeChars.charAt(c1 >> 2);
      out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
      out += base64EncodeChars.charAt((c2 & 0xF) << 2);
      out += "=";
      break;
    }
    c3 = str.charCodeAt(i++);
    out += base64EncodeChars.charAt(c1 >> 2);
    out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
    out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
    out += base64EncodeChars.charAt(c3 & 0x3F);
  }
  return out;
}


function utf16to8(str) {
  var out, i, len, c;
  out = "";
  len = str.length;
  for(i = 0; i < len; i++) {
    c = str.charCodeAt(i);
    if ((c >= 0x0001) && (c <= 0x007F)) {
      out += str.charAt(i);
    } else if (c > 0x07FF) {
      out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
      out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
      out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
    } else {
      out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
      out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
    }
  }
  return out;
}

function encodeTask() {
  var form = document.taskPerformForm;
  form.taskContent.value = base64encode(utf16to8(encodeURIComponent(form.taskText.value)));
  form.taskText.disabled = true;
}
</script>
</head>
<body>
<div id="tableMain">
     <div id="message"><%=PageUtils.errors(action)%></div>
     <div id="condition">
         <form name="taskPerformForm" method="post" action="sqlExecute.htm"  onsubmit="encodeTask()">
         
           <div style="margin-bottom:5px;">支持/* */注释, -- 开头注释, &#36;{param name} 参数模式</div>
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
              <tr>
                 <td><textarea name="taskText" style="width:99%;height:120px;" onkeyup="parseParam()"><%=StringUtils.htmlFilterToEmpty(action.getTaskContent())%></textarea></td>
              </tr>
            </table>
             <input type="hidden" name="resultIndex" value="0"/>
            <input type="hidden" name="taskContent" value="" />
            <input type="submit" name="Submit" class="colorButton" value=" 执行 " />
            <br />
            <div id="paramDiv" style="display:none"></div>
                                 请输入口令：
            <input name="key" type="text" size="40" value="<%=PageUtils.ignoreNull(action.getKey())%>" /> 
         </form>
     </div>
     <div id="pageNumber"></div>
     <% if (!action.hasErrors() && action.isSelect()) {
         int resultLength = action.getResultPacks().length;
         ResultPack resultPack = action.getCurrentResultPack();
     %>
        <div id="titleMain">
           <%for (int i = 0; i < resultLength; i++){ %>
                <a href="javascript:showResultPage('<%=i%>')">
                  <% if (i == action.getResultIndex()) { %>
                        <div id="current">Result <%=i + 1%></div><%
                   }else{ %>
                        <div class="other">Result <%=i + 1 %></div><%
                   } %>
                </a>
           <%} %>
        </div>
        <%if (resultPack != null){ %>
            <div style="overflow-x:auto; overflow-y:auto; width:98%; height:220px">
               <table id="dataTable" border="0" align="center" cellpadding="5" cellspacing="1" style="margin-top:0px;">
                 <tr class="titleTr">
                           <td>&nbsp;</td>
                     <%int columnCount = resultPack.getColumnCount();
                       String[] columnNames = resultPack.getColumnNames();
                       for (int i = 0; i < columnCount; i++) { %>
                           <td><%=columnNames[i]%></td>
                      <%} %> 
                 </tr>
                 <%List<String[]> records = resultPack.getRecords();
                     for (int i = 0; i < records.size(); i++) {
                      String[] record = records.get(i);%>
                      <tr>
                          <td><%=i + 1%></td>
                       <%for (int j = 0; j < columnCount; j++) { %>
                          <td nowrap="nowrap" title="<%=columnNames[j] %>"><%=record[j]%></td>
                       <%} %>
                      </tr>
                 <%} %>
               </table>
            </div>
        <%}else { %>
             <table id="dataTable" border="0" align="center" cellpadding="5" cellspacing="1">
               <tr class="titleTr"> <td>&nbsp;</td> </tr>
             </table>
        <%} %>
     <%}else if (!action.hasErrors() && !action.isSelect() && null != action.getResultCodes()){ 
            int[] resultCodes = action.getResultCodes();
            for (int i = 0; i < resultCodes.length; i++) {
     %>
               <div id="message"><span><%=i + 1%>、更新了<%=resultCodes[i]%>条记录</span></div>
               <br />
            <%} %>
     <%} %>
</div>
<iframe name="iframe" id="iframe"  frameborder="0" style="display:none"></iframe>
<script type="text/javascript" language="javascript">
parseParam();
</script>
</body>
</html>
